<?php
class PreferenceAction extends Action{
 public function _initialize(){//初始化
		R("Home.Public","top");
		R("Home.Public","bottom");
  }
  public function index(){
	 import("ORG.Com.DiyCache");
	 $mycache = new DiyCache;
	 if(!$mycache->is_cached()){//处理自定义缓存
	    $coupon = M("coupon");
		Vendor("LJW.page");
		$count = $coupon->count();
		$mypage = new page($count,9,3,U('Preference/index'));
		 //分页
		  //上一页   下一页 
		 if($mypage->absolutepage <> 1) $prev_page="<span class='next_bz'><a href='".$mypage->url."/p/".($mypage->absolutepage-1)."'>上一页</a></span>";	
		 if($mypage->absolutepage <> $mypage->pagenum) $next_page="<span class='next_bz'><a href='".$mypage->url."/p/".($mypage->absolutepage+1)."'>下一页</a></span>";
		 
		  
		 $start=$mypage->absolutepage-4;
		 if($start<1)$start=1;
		 $end=$start+8;
		 if($end>$mypage->pagenum) $end=$mypage->pagenum;
			 
		 $start_url=$mypage->url."/p/".$start;
		 $end_url=$mypage->url."/p/".$end;
			 
		 $f_url=$mypage->url."/p/1";//第一页
		 $l_url=$mypage->url."/p/".$mypage->pagenum;//最后一页
			 
		 if($start<>1)$f_str="<a href='$f_url'>1</a><a href='#'>…</a>";
		 if($end<>$mypage->pagenum)$l_str="<a href='#'>…</a><a href='$l_url'>".$mypage->pagenum."</a>";
			 
		 for($i=$start;$i<=$end;$i++){
			if($mypage->absolutepage == $i){
			   $m_str.="<a href='".$mypage->url."/p/".$i."' class='on_link' >$i</a>";
			}else{
			   $m_str.="<a href='".$mypage->url."/p/".$i."'>$i</a>";	
			}
			
		 }
		 $div_str.=$prev_page.$f_str.$m_str.$l_str.$next_page;
		 
	     $this->assign("divpage",$div_str);
		 
		 $c_list = $coupon->limit($mypage->start.",".$mypage->pagesize)->order("sequence desc")->findAll();
		 foreach($c_list as $k=>$v){
			 $rank = $v["rank"];
			 $c_list[$k]['rank_img'] = get_images_by_rank($rank);
		 }
		 
		 $this->assign("c_list",$c_list);
		 
	    if(C("diy_cache"))$mycache->create_cache($this->fetch());//创建缓存 
	    $this->display();  
	 }
  }
  
  public function print_pre(){//打印优惠券
        /*安全处理*/
		checkget();
		/*安全处理*/
		
		$coupon=M("coupon");
		$p_num_str=explode(",",$_GET["p_num_str"]);
		$p_id_str=explode(",",$_GET["p_id_str"]);
		foreach($p_id_str as $k=>$v){
			$cp_big_pic=$coupon->where("id=$v")->getField("cp_big_pic");
			for($i=1;$i<=$p_num_str[$k];$i++){
				$cp_arr[]=$cp_big_pic;
			}
		}
		$group_num=ceil(count($cp_arr)/7);
		for($i=1;$i<=$group_num;$i++){
			$start=($i-1)*6;
			$group_arr[]=array_slice($cp_arr,$start,6);
		}
		$this->assign("group_arr",$group_arr);
		$this->display();
	}
	
}
function get_images_by_rank($rank){
	$str='<img src="'.WEB_PUBLIC_PATH.'/images/home/xinxin1.jpg" width="12" height="12" border="0" alt="">';
	$no_select_str='<img src="'.WEB_PUBLIC_PATH.'/images/home/xinxin2.jpg" width="12" height="12" border="0" alt="">';
	switch($rank){
	  case "1级":
	    $return_str = str_repeat($str,1).str_repeat($no_select_str,4);
	  break;	
	  
	  case "2级":
	    $return_str = str_repeat($str,2).str_repeat($no_select_str,3);
	  break;
	  
	  case "3级":
	    $return_str = str_repeat($str,3).str_repeat($no_select_str,2);
	  break;
	  
	  case "4级":
	    $return_str = str_repeat($str,4).str_repeat($no_select_str,1);
	  break;
	  
	  case "5级":
	    $return_str = str_repeat($str,5).str_repeat($no_select_str,0);
	  break;
	}
	return $return_str;
}
?>